package cn.wenzy.aurora.framework.support.i18n;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import java.util.Locale;
import java.util.Optional;

@AllArgsConstructor
public class I18nSupport{

    private MessageSource messageSource;

    public String getMessage(String code, Object... args) {
        return getMessage(code, code, null, args);
    }

    public String getMessage(String code, String defaultMessage, Object... args) {
        return getMessage(code, defaultMessage, null, args);
    }


    private String getMessage(String code, String defaultMessage, Locale locale, Object... args) {
        if (StrUtil.isBlank(code)) {
            return defaultMessage;
        }
        return messageSource.getMessage(code, args, defaultMessage, locale);
    }
}
